package cn.zjh.org.controller;

import cn.zjh.basic.util.AjaxResult;
import cn.zjh.basic.util.PageList;
import cn.zjh.org.domain.ShopAuditLog;
import cn.zjh.org.query.ShopAuditLogQuery;
import cn.zjh.org.service.IShopAuditLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "上传图片的api",description = "图片上传和删除")
@RequestMapping("/shopAuditLog")
@RestController
public class ShopAuditLogController {

    @Autowired
    private IShopAuditLogService shopAuditLogService;

    /**
     * 查询单个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个")
    public ShopAuditLog findOne(@PathVariable("id") Long id) {
        return shopAuditLogService.findOne(id);
    }

    /**
     * 查询所有
     * @return
     */
    @ApiOperation(value = "查询所有")
    @GetMapping
    public List<ShopAuditLog> findAll() {
        return shopAuditLogService.findAll();

    }

    /**
     * 分页查询
     * @param shopAuditLogQuery
     * @return
     */
    @ApiOperation(value = "分页查询")
    @PostMapping
    public PageList<ShopAuditLog> queryPage(@RequestBody ShopAuditLogQuery shopAuditLogQuery){
        return shopAuditLogService.queryPage(shopAuditLogQuery);
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopAuditLogService.del(id);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 添加或修改
     * @param shopAuditLog
     * @return
     */
    @ApiOperation(value = "添加或修改")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody ShopAuditLog shopAuditLog){
        try {
            if(shopAuditLog.getId() == null){//添加
                shopAuditLogService.save(shopAuditLog);
            }else{
                shopAuditLogService.upDate(shopAuditLog);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 接口：批量删除
     * @param
     * @return AjaxResult 响应给前端
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult patchDelete(@RequestBody Long[] ids){
        try {
            shopAuditLogService.patchDelete(ids);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"批量删除失败");
        }
    }
}
